﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_BulkProduct
    {
        public bool CreateOneBulkProduct(BulkProduct_Info objBulkProduct_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"BulkProductID", objBulkProduct_Info.BulkProductID));
            command.Parameters.Add(new SqlParameter(@"FarmID", objBulkProduct_Info.FarmID));
            command.Parameters.Add(new SqlParameter(@"BulkProductName", objBulkProduct_Info.BulkProductName));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneBulkProduct(BulkProduct_Info objBulkProduct_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@BulkProductID", objBulkProduct_Info.BulkProductID));
            command.Parameters.Add(new SqlParameter("@FarmID", objBulkProduct_Info.FarmID));
            command.Parameters.Add(new SqlParameter("@BulkProductName", objBulkProduct_Info.BulkProductName));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneBulkProduct(int BulkProductID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"BulkProductID", BulkProductID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllBulkProduct()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public BulkProduct_Info GetOneBulkProduct(int BulkProductID)
        {
            BulkProduct_Info objBulkProduct_Info = new BulkProduct_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("BulkProduct_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"BulkProductID", BulkProductID));
            try
            {
                objConnection.Open();
                SqlDataReader ordBulkProduct = command.ExecuteReader(); //OjectRecordDatareader
                ordBulkProduct.Read();
                if (ordBulkProduct.HasRows)
                {
                    if (ordBulkProduct["BulkProductID"] != DBNull.Value)
                    {
                        objBulkProduct_Info.BulkProductID = Convert.ToInt16(ordBulkProduct["BulkProductID"]);
                    }
                    if (ordBulkProduct["FarmID"] != DBNull.Value)
                    {
                        objBulkProduct_Info.FarmID = Convert.ToInt16(ordBulkProduct["FarmID"]);
                    }
                    if (ordBulkProduct["BulkProductName"] != DBNull.Value)
                    {
                        objBulkProduct_Info.BulkProductName = Convert.ToString(ordBulkProduct["BulkProductName"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objBulkProduct_Info;
        }
    }
}
